package ge.gen.eBdgt.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ProjUtils {
	public static String formatDate(String dt) {
		if (isNotNull(dt)) {
			DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
			Date today = null;
			try {
				today = df.parse(dt);
			} catch (Exception e) {
				return dt;
			}
			return df.format(today);
		}
		return dt;
	}

	protected static boolean isNotNull(String o) {
		if (o != null && o.length() > 0)
			return true;
		return false;
	}

	private static boolean isNull(String o) {
		if (o == null || o.length() <= 0)
			return true;
		return false;
	}

	public static int getMonth(String dt) {
		if (isNotNull(dt)) {
			Date newDt = new Date(dt);
			if (newDt == null)
				return -10;
			return newDt.getMonth();
		}
		return -10;
	}

	public boolean IsMonthWithinYearPeriod(int mn, int yr, String outDt) throws Exception {
		if (isNotNull(outDt) == false)
			return false;
		Date dt = new Date(outDt);		
		Calendar cal = Calendar.getInstance();  
		cal.setTime(dt);
		dt =  cal.getTime();
		cal.set(yr, mn, 1);	   
	    Date minDt= cal.getTime();
	    cal.add(Calendar.MONTH, 12);
	    cal.add(Calendar.DAY_OF_MONTH, -1);
	    Date maxDt= cal.getTime();
	    if(minDt.compareTo(dt) <= 0 && maxDt.compareTo(dt) >= 0){
	    	return true;
	    }
	    return false;
	}
	
	public static int[] addMonths(int mn, int yr) {
		int[] yrMonth = new int[2];
		Calendar c1 = Calendar.getInstance();
		c1.set(yr, mn, 1);
		c1.add(Calendar.MONTH, 11);
		yrMonth[0] = c1.get(Calendar.MONTH);
		yrMonth[1] = c1.get(Calendar.YEAR);
		return yrMonth;
	}

	public static int[] getMonthYear(int mn, int yr, String dt) {
		int[] yrMonth = new int[2];

		try {
			if (isNotNull(dt)) {
				Date newDt = new Date(dt);
				if (newDt == null)
					return null;
				int endMnthyr[] = addMonths(mn, yr);
				SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy");
				yrMonth[0] = newDt.getMonth();
				yrMonth[1] = Integer.parseInt(simpleDateformat.format(newDt));
				if(yrMonth[1] != yr)
					yrMonth[0] = yrMonth[0] + 12;			
				return yrMonth;
			}
		} catch (Exception e) {
			return null;
		}
		return null;
	}

	public Date getDate(int startMonth, int year) throws Exception {
		SimpleDateFormat simpleDateformat = new SimpleDateFormat("m-dd-yyyy");
		String dt = (startMonth+1) + "-01-" + year;
		
		return simpleDateformat.parse(dt);
	}

}
